package com.bqtt.tumor.demo.entity;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.Size;

import org.tbwork.utility.toolbox.tbConvert;

import com.bqtt.tumor.demo.business.BodyPartService;
import com.bqtt.tumor.demo.business.CityService;
import com.bqtt.tumor.demo.business.DeseaseService;
import com.bqtt.tumor.demo.business.FeatureService;
import com.bqtt.tumor.demo.business.HealthUserService;
import com.bqtt.tumor.demo.business.OccupationService;
import com.bqtt.tumor.demo.business.TreatmentMethodService;
import com.bqtt.tumor.demo.common.utility.InlineTools;
import com.bqtt.tumor.demo.presentation.model.desease.DCharacter;
 


@Entity
@Table(name="medical_case") 
public class MedicalCase  implements IDBEntity{

	@Id 
    @Column(name="medical_case_id")    
    @GeneratedValue(strategy=GenerationType.IDENTITY) 
	private Integer medical_case_id;

	
	@Column(name = "health_user_id_number", length= 22)
	@Size(min=18,max=22,message="长度为18-22个数字")
	private String health_user_id_number;
	
	@Column(name = "desease_code", length= 25)
	@Size(min=1,max=25,message="长度为1-25个字符")
	private String desease_code;
	

	@Column(name = "occur_datetime")
	private Timestamp occur_datetime;
	
	@Column(name = "discover_datetime")
	private Timestamp discover_datetime;
	
	@Column(name = "treatment_start_datetime")
	private Timestamp treatment_start_datetime;
	
	@Column(name = "treatment_end_datetime")
	private Timestamp treatment_end_datetime;

	@Column(name = "effect_score")
	private int effect_score;
	
	@Column(name = "clinic_feature", length= 1000)
	private String clinic_feature;
	
	@Column(name = "treatment_method_id")
	private Integer treatment_method_id;
	

	// health user name
	
	@Transient
	private String health_user_name;
	
	@Transient
	private String desease_name;

	@Transient
	private String treatment_start_datetime_string;

	
	@Transient
	private String treatment_end_datetime_string;
	
	@Transient
	private String occur_datetime_string;
	
	@Transient
	private String discover_datetime_string;
	
	@Transient
	private String treatment_method_name;
	
	@Transient
	private String displayName=""; // for mvc
	
	
	// 临床特征
	@Transient
	private List<Feature> potential_clinic_list;

	//治疗方法
	@Transient
	private List<TreatmentMethod>  potential_treatment_method_list;
	
	
	public MedicalCase() {
		super();
		displayName="none";
		// TODO Auto-generated constructor stub
	}


	public Integer getMedical_case_id() {
		return medical_case_id;
	}


	public void setMedical_case_id(Integer medical_case_id) {
		this.medical_case_id = medical_case_id;
	}


	public String getHealth_user_id_number() {
		return health_user_id_number;
	}

	public void setHealth_user_id_number(String health_user_id_number) {
		this.health_user_id_number = health_user_id_number;
	}

	public String getDesease_code() {
		return desease_code;
	}

	public void setDesease_code(String desease_code) {
		this.desease_code = desease_code;
	}

	public Timestamp getOccur_datetime() {
		return occur_datetime;
	}

	public void setOccur_datetime(Timestamp occur_datetime) {
		this.occur_datetime = occur_datetime;
	}

	public Timestamp getDiscover_datetime() {
		return discover_datetime;
	}

	public void setDiscover_datetime(Timestamp discover_datetime) {
		this.discover_datetime = discover_datetime;
	}

	public Timestamp getTreatment_start_datetime() {
		return treatment_start_datetime;
	}

	public void setTreatment_start_datetime(Timestamp treatment_start_datetime) {
		this.treatment_start_datetime = treatment_start_datetime;
	}

	public Timestamp getTreatment_end_datetime() {
		return treatment_end_datetime;
	}

	public void setTreatment_end_datetime(Timestamp treatment_end_datetime) {
		this.treatment_end_datetime = treatment_end_datetime;
	}

	public int getEffect_score() {
		return effect_score;
	}

	public void setEffect_score(int effect_score) {
		this.effect_score = effect_score;
	}
	

	public String getHealth_user_name() {
		return health_user_name;
	}


	public void setHealth_user_name(String health_user_name) {
		this.health_user_name = health_user_name;
	}


	public String getDesease_name() {
		return desease_name;
	}


	public void setDesease_name(String desease_name) {
		this.desease_name = desease_name;
	}

	public String getTreatment_method_name() {
		return treatment_method_name;
	}


	public void setTreatment_method_name(String treatment_method_name) {
		this.treatment_method_name = treatment_method_name;
	}


	public String getClinic_feature() {
		return clinic_feature;
	}


	public void setClinic_feature(String clinic_feature) {
		this.clinic_feature = clinic_feature;
	}


	public Integer getTreatment_method_id() {
		return treatment_method_id;
	}


	public void setTreatment_method_id(Integer treatment_method_id) {
		this.treatment_method_id = treatment_method_id;
	}


	public List<Feature> getPotential_clinic_list() {
		return potential_clinic_list;
	}


	public void setPotential_clinic_list(List<Feature> potential_clinic_list) {
		this.potential_clinic_list = potential_clinic_list;
	}


	public List<TreatmentMethod> getPotential_treatment_method_list() {
		return potential_treatment_method_list;
	}


	public void setPotential_treatment_method_list(
			List<TreatmentMethod> potential_treatment_method_list) {
		this.potential_treatment_method_list = potential_treatment_method_list;
	}


	public String getTreatment_start_datetime_string() {
		return treatment_start_datetime_string;
	}


	public void setTreatment_start_datetime_string(
			String treatment_start_datetime_string) {
		this.treatment_start_datetime_string = treatment_start_datetime_string;
	}


	public String getTreatment_end_datetime_string() {
		return treatment_end_datetime_string;
	}


	public void setTreatment_end_datetime_string(
			String treatment_end_datetime_string) {
		this.treatment_end_datetime_string = treatment_end_datetime_string;
	}


	public String getOccur_datetime_string() {
		return occur_datetime_string;
	}


	public void setOccur_datetime_string(String occur_datetime_string) {
		this.occur_datetime_string = occur_datetime_string;
	}


	public String getDiscover_datetime_string() {
		return discover_datetime_string;
	}


	public void setDiscover_datetime_string(String discover_datetime_string) {
		this.discover_datetime_string = discover_datetime_string;
	}


	public String getDisplayName() {
		return displayName;
	}


	public void setDisplayName(String displayName) {
		this.displayName = displayName;
	}


	/**  获取SQL有效的非空的字段
	 * @return
	 */
	public Map<String,Object> getSQLValidAttributes()
	{
		Map<String,Object> result = new HashMap<String,Object>();
		
		if(!(health_user_id_number==null || health_user_id_number.isEmpty()))
		{
			result.put("health_user_id_number", health_user_id_number);
		}
		
		return result;
	}
	
	
	/**
	 *  把一些code编号 转变为实际的字符串
	 */
	public void fillStringField()
	{
		List<HealthUser> healthUserList = HealthUserService.getHealthUserDAO().findByProperty("id_number", health_user_id_number);
		
		if(healthUserList!=null && healthUserList.size()>0)
		{
			health_user_name = healthUserList.get(0).getName();
		}
		
		if(desease_code !=null && !desease_code.isEmpty())
		{
			desease_name = DeseaseService.getDeseaseNameByCode(desease_code);
		}
		
		if(occur_datetime!=null)
		{
			occur_datetime_string = InlineTools.getDateString(tbConvert.Timestamp2String(occur_datetime));
		}
		
		if(discover_datetime!=null)
		{
			discover_datetime_string = InlineTools.getDateString(tbConvert.Timestamp2String(discover_datetime));
		}
		
		
		if(treatment_start_datetime!=null)
		{
			treatment_start_datetime_string = InlineTools.getDateString(tbConvert.Timestamp2String(treatment_start_datetime));
		}
		
		if(treatment_end_datetime!=null)
		{
			treatment_end_datetime_string = InlineTools.getDateString(tbConvert.Timestamp2String(treatment_end_datetime));
		}
		
		
		List<Integer> featureIds = DeseaseService.getFeatureIdsByDeseaseCode(desease_code);
		List<Feature> featureList = FeatureService.getFeaturesByIds(featureIds) ; 
		
		//临床特征填充
		if(!InlineTools.isEmpty(clinic_feature))
		{
			String [] array = clinic_feature.split("\\|");
			for(Feature item: featureList)
			{
				if(InlineTools.containsItem(array, item.getFeature_id()+""))
				{
					item.setChecked(InlineTools.checkedStyle);
				}
				else
				{
					item.setChecked(InlineTools.nocheckedStyle);
				}
			}
		}
		else
		{
			for(Feature item: featureList)
			{
					item.setChecked(InlineTools.nocheckedStyle);
			}
		}
		setPotential_clinic_list(featureList);
		
		//获取该疾病所有治疗方法
		List<Integer> method_ids = DeseaseService.getTreatmentMethodIdsByDeseaseCode(desease_code);

		setPotential_treatment_method_list(TreatmentMethodService.getMethodsByIds(method_ids));
	
		if(treatment_method_id!=null && treatment_method_id!=0)
		{
			TreatmentMethod temp = TreatmentMethodService.getMethodById(treatment_method_id);
			
			if(temp!=null)
			  setTreatment_method_name(temp.getMethod_name());
		}
		
	}
	
	
	public void fillDBfields()
	{
		if(!InlineTools.isEmpty(occur_datetime_string))
		{
			Timestamp temp =null;
			try{
				temp= tbConvert.StringToTimestamp(occur_datetime_string+" 00:00:00");
			}
			catch(Exception e)
			{
				temp = new Timestamp(0);
			}
			
			occur_datetime =temp;
		}
		if(!InlineTools.isEmpty(discover_datetime_string))
		{
			Timestamp temp =null;
			try{
				temp= tbConvert.StringToTimestamp(discover_datetime_string+" 00:00:00");
			}
			catch(Exception e)
			{
				temp = new Timestamp(0);
			}
			
			discover_datetime =temp;
		}
		if(!InlineTools.isEmpty(treatment_start_datetime_string))
		{
			Timestamp temp =null;
			try{
				temp= tbConvert.StringToTimestamp(treatment_start_datetime_string+" 00:00:00");
			}
			catch(Exception e)
			{
				temp = new Timestamp(0);
			}
			
			treatment_start_datetime =temp;
		}
		if(!InlineTools.isEmpty(treatment_end_datetime_string))
		{
			Timestamp temp =null;
			try{
				temp= tbConvert.StringToTimestamp(treatment_end_datetime_string+" 00:00:00");
			}
			catch(Exception e)
			{
				temp = new Timestamp(0);
			}
			
			treatment_end_datetime =temp;
		}
	}
}
